<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .box {
        width: 100px;
        height: 100px;
        background: cyan;
        border-radius: 50%;
        position: absolute;
        left: 0px;
        transition: all 2s;
      }
    </style>
  </head>
  <body>
    <div class="box"></div>
    <script>
      var boxEle = document.querySelector(".box");
      //获取到div元素
      boxEle.onclick = function () {
        //绑定点击事件
        var res = innerWidth - this.offsetWidth; //用浏览器宽减去我们点击的div的水平方向 width + 左右padding + 左右border-width
        this.style.left = res + "px"; //给div添加left属性
        var that = this; //下面函数中使用this指向window,所以我们把this换个变量继承
        setTimeout(function () {
          //   console.log(111);
          //   this.style.left = "0px"; //这里this指向window
          that.style.left = "0px"; //当div元素执行2s后再跳回0px位置
        }, 2000); //setTime值最后是时间ms
      };
    </script>
  </body>
</html>
